package org.lzfto.flink.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lzfto.flink.demo.business.flinkTest.base.BasicTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.BasicJdbcTable;
import org.lzfto.flink.demo.common.result.PubResult;
import org.lzfto.flink.demo.config.AppConfiguration;
import org.lzfto.flink.demo.init.InitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 初始化bean，应该在系统启动之后处理
 */
@Lazy
@RestController
@RequestMapping("/bean")
public class BeanController {
	@Autowired
	private InitService initService;


	@GetMapping("/getRegisterStatus")
	public PubResult<Map<String,Boolean>> isRegister() {
		Map<String,Boolean> map=new HashMap<>();
		map.put("datasource", AppConfiguration.getConf().getIsDatasourceRegistered());
		map.put("artermis", AppConfiguration.getConf().getIsArtemisRegistered());
		return PubResult.ok("成功",map);
	}
	
	@SuppressWarnings("rawtypes")
	@PostMapping("register-ds")
	public PubResult registerDatasource(String userName,String password) {
		AppConfiguration config=AppConfiguration.getConf();
		config.setDbUserName(userName);
		config.setDbPassword(password);
		List<BasicTable> tables=config.getTables();
		if (tables!=null && tables.size()>0) {
			for(BasicTable table:tables) {
				if (table instanceof BasicJdbcTable  jdbcTable) {
					jdbcTable.setUserName(userName);
					jdbcTable.setPassword(password);
				}
			}
		}
		return initService.registerDatasource();
	}
	
	@SuppressWarnings("rawtypes")
	@PostMapping("register-mqtt")
	public PubResult registerArtermis(String host,String userName,String password) {
		AppConfiguration config=AppConfiguration.getConf();
		config.setArtemisHost(host);
		config.setArtemisUserName(userName);
		config.setArtemisPassword(password);
		return initService.registerArtermis();
	}

}
